<!DOCTYPE html>
<html>

<head>
  <title>变量</title>
  <script type="text/javascript">
  // 1.交换变量的数值
  let x = 1;
  let y = 2;
  [x, y] = [y, x];

  console.log(x, y);

  // 2.从函数返回多个值
  //返回一个数组
  function example() {
    return [1, 2, 3];
  }
  let [a, b, c] = example();


  //返回一个对象
  function example2() {
    return {
      foo: 'foo',
      bar: 'bar'
    }
  }
  let { foo, bar } = example2();

  console.log(foo, bar, a, b, c);

  //函数参数的定义：解构赋值可以方便地将一组参数与变量名对应起来。
  // 参数是一组有次序的值
  function f([x, y, z]) {
    console.log(x, y, z);
  }
  f([1, 2, 3]);

  // 参数是一组无次序的值
  function f2({ x, y, z }) {
    console.log(x, y, z);
  }
  f2({ z: 3, y: 2, x: 1 });

  //提取JSON数据
  let json = {
    id: 42,
    status: 'OK',
    data: [100, 200]
  };

  let { id: pid, status: sta, data: number } = json;
  console.log(pid, sta, number);

  //函数参数的默认值
  function f3(url, {
    name = 'fujie',
    age = 28,
    doing = function() {
      console.log('doing');
    },
  } = {}) {
    console.log(url, name, age, doing);
  }

  f3('localhost://url', { name: 'heyan' });

  //遍历Map结构
  const map = new Map();
  map.set('first', 'hello');
  map.set('second', 'world');

  for (let [key, value] of map) {
    console.log(`${key} is ${value}`);
  }
  </script>
</head>

<body>
</body>

</html>